package com.example.journalegocentrique;

import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

//Helper class to insert elements in the initial ListView

public class DayAdapter extends ArrayAdapter<Day>{

      private int resource;
      private LayoutInflater inflater;
      private Context context;

      public DayAdapter ( Context ctx, int resourceId, List<Day> objects) {

            super( ctx, resourceId, objects );
            resource = resourceId;
            inflater = LayoutInflater.from( ctx );
            context=ctx;
      }

      @Override
      public View getView ( int position, View convertView, ViewGroup parent ) {

            //Creates the view of the layout and sets it to the row
            convertView = ( RelativeLayout ) inflater.inflate( resource, null );

            //Extracts the day to create
            Day day = (Day) getItem(position);

            //Takes the TextView of the layout and sets the day
            TextView txtDate = (TextView) convertView.findViewById(R.id.day);
            txtDate.setText(day.getDate());

            //Takes the TextView of the layout and sets the hour
            TextView txtHour = (TextView) convertView.findViewById(R.id.hour);
            txtHour.setText(day.getHour());

            //Takes the ImageView of the layout and sets the day's image
            ImageView imageDay = (ImageView) convertView.findViewById(R.id.ImageDay);
            String uri = "drawable/" + day.getImage();
            int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
            Drawable image = context.getResources().getDrawable(imageResource);
            imageDay.setImageDrawable(image);
            return convertView;
      }
}
